using Microsoft.Practices.SmartClient.DisconnectedAgent;
using System;

namespace WinFormCD.Infrastructure.Library.DSA.Service
{
    // Generated code for the web service.
    // Use this proxy to make requests to the service when working in an application that is occasionally connected
    public partial class Agent
    {
        IRequestQueue requestQueue;

        public Agent(IRequestQueue requestQueue)
        {
            this.requestQueue = requestQueue;
        }

        #region HelloWorld

        /// <summary>
        /// Enqueues a request to the <c>HelloWorld</c> web service method through the agent.
        /// </summary>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid HelloWorld()
        {
            return HelloWorld(GetHelloWorldDefaultBehavior());
        }

        /// <summary>
        /// Enqueues a request to the <c>HelloWorld</c> web service method through the agent.
        /// </summary>
        /// <param name="behavior">The behavior associated with the offline request being enqueued.</param>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid HelloWorld(OfflineBehavior behavior)
        {
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnHelloWorldReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnHelloWorldException");

            return EnqueueRequest("HelloWorld", behavior);
        }

        public static OfflineBehavior GetHelloWorldDefaultBehavior()
        {
            OfflineBehavior behavior = GetAgentDefaultBehavior();
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnHelloWorldReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnHelloWorldException");

            return behavior;
        }

        #endregion HelloWorld

        #region Common

        public static OfflineBehavior GetAgentDefaultBehavior()
        {
            OfflineBehavior behavior = new OfflineBehavior();
            behavior.MaxRetries = 3;
            behavior.Stamps = 1;
            behavior.Expiration = DateTime.Now + new TimeSpan(1, 0, 0, 0);
            behavior.ProxyFactoryType = typeof(Microsoft.Practices.SmartClient.DisconnectedAgent.WebServiceProxyFactory);

            return behavior;
        }

        private Guid EnqueueRequest(string methodName, OfflineBehavior behavior, params object[] arguments)
        {
            Request request = CreateRequest(methodName, behavior, arguments);

            requestQueue.Enqueue(request);

            return request.RequestId;
        }

        private static Request CreateRequest(string methodName, OfflineBehavior behavior, params object[] arguments)
        {
            Request request = new Request();
            request.MethodName = methodName;
            request.Behavior = behavior;
            request.CallParameters = arguments;

            request.OnlineProxyType = OnlineProxyType;
            request.Endpoint = Endpoint;

            return request;
        }

        public static Type OnlineProxyType
        {
            get
            {
                return typeof(WinFormCD.Infrastructure.Library.HelloWorld.Service);
            }
        }

        public static string Endpoint
        {
            get
            {
                return String.Empty;
            }
        }
        #endregion
    }
}
